@page "/policies"
@using PolicySearchService.Api.Queries.Dtos
@using Microsoft.AspNetCore.Authorization

@attribute [Authorize]

<Row>
    <Column>
            <Card>
                <CardHeader>
                    <CardTitle>Search policies</CardTitle>
                </CardHeader>
                <CardBody>
                    <Row>
                        <Column ColumnSize="ColumnSize.Is5">
                            <Field Horizontal>
                                <FieldLabel ColumnSize="ColumnSize.Is2">Policy number</FieldLabel>
                                <FieldBody ColumnSize="ColumnSize.Is10">
                                    <TextEdit @bind-Text="@CriteriaModel.PolicyNumber"></TextEdit>
                                </FieldBody>
                            </Field>
                        </Column>
                        <Column ColumnSize="ColumnSize.Is5">
                            <Field Horizontal>
                                <FieldLabel ColumnSize="ColumnSize.Is2">Policy holder</FieldLabel>
                                <FieldBody ColumnSize="ColumnSize.Is10">
                                    <TextEdit @bind-Text="@CriteriaModel.PolicyHolder"></TextEdit>
                                </FieldBody>
                            </Field>
                        </Column>
                        <Column ColumnSize="ColumnSize.Is2">
                            <Button Color="Color.Primary" @onclick="Search">Search</Button>
                        </Column>
                    </Row>
                </CardBody>
                
                @if (Policies.Any())
                {
                    <CardBody>
                        <Table Bordered Striped>
                            <TableHeader>
                                <TableHeaderCell>Policy number</TableHeaderCell>
                                <TableHeaderCell>Product</TableHeaderCell>
                                <TableHeaderCell>Policy start</TableHeaderCell>
                                <TableHeaderCell>Policy end</TableHeaderCell>
                                <TableHeaderCell>Policy holder</TableHeaderCell>
                                <TableHeaderCell>Price</TableHeaderCell>
                            </TableHeader>
                            @foreach (var policy in Policies)
                            {
                                <TableRow>
                                    <TableRowCell>@policy.PolicyNumber</TableRowCell>
                                    <TableRowCell>@policy.ProductCode</TableRowCell>
                                    <TableRowCell>@policy.PolicyStartDate</TableRowCell>
                                    <TableRowCell>@policy.PolicyEndDate</TableRowCell>
                                    <TableRowCell>@policy.PolicyHolder</TableRowCell>
                                    <TableRowCell>@policy.PremiumAmount EUR</TableRowCell>
                                </TableRow>
                            }
                        </Table>
                    </CardBody>
                }
            </Card>
    </Column>
</Row>




@code {
    private List<PolicyDto> Policies { get; set; } = new List<PolicyDto>() { };
    private PolicySearchCriteriaModel CriteriaModel { get; set; } = new();
    [Inject] 
    protected PolicySearchClient PolicySearchClient { get; set; }
    
    protected async Task Search()
    {
        var searchResult = await PolicySearchClient.SearchPolicies
        (
            CriteriaModel.PolicyNumber,
            CriteriaModel.PolicyHolder
        );

        if (searchResult.IsError)
        {
            
        }

        Policies = searchResult.Result.Policies;

    }
}